package com.wl.api.controller.system;

import java.util.Objects;
import java.util.function.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.CollectionUtil;
import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.ObjectUtil;
import com.wl.api.common.utils.RWYException;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.system.SecurityGradeDTO;
import com.wl.api.dto.system.SecurityGradePriceDTO;
import com.wl.api.model.system.SecurityGrade;
import com.wl.api.model.system.SecurityGradePrice;
import com.wl.api.service.system.ISecurityGradePriceService;
import com.wl.api.service.system.ISecurityGradeService;

/**
 * 一个等级对应多个渠道的费用类型折扣
 * */
@RestController
@RequestMapping("/admin/grade/discount")
public class AdminGradeDiscountController extends CommonController {

	@Autowired
	ISecurityGradeService gradeService;
	@Autowired
	ISecurityGradePriceService gradePriceService;
	
	@RequestMapping(value="/manage", method=RequestMethod.POST)
	public JsonObject create(@RequestBody SecurityGradeDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "", code = "";
		if (validate().test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if(pass) {
			initData(param);
			json.setData(gradeService.insertGradePrice(param));
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	/**
	 *  修改产品数据
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/manage", method = RequestMethod.PUT)
	public JsonObject modify(@RequestBody SecurityGradeDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		if (validate().or(s -> s.getId() == null).test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			error_code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if(pass) {
			initData(param);
			gradeService.updateGradePrice(param);
			json.setData(param.getId());
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	
	@RequestMapping(value="/manage", method = RequestMethod.GET)
	public JsonObject getItemList(SecurityGradeDTO param) {
		JsonObject json = JsonObject.getInstance();
		json.setPage(gradeService.getItemList(param));
		setJsonMessage(json);
		return json;
		
	}
	
	@RequestMapping(value="/allGrade", method = RequestMethod.GET)
	public JsonObject getGradeList() {
		JsonObject json = JsonObject.getInstance();
		json.setData(gradeService.selectList(new EntityWrapper<SecurityGrade>()));
		setJsonMessage(json);
		return json;
		
	}
	
	
	@RequestMapping(value="/manage/{gradeId}", method = RequestMethod.GET)
	public JsonObject getDetail(@PathVariable("gradeId")Long gradeId) throws Exception{
		JsonObject json = JsonObject.getInstance();
		SecurityGrade grade =  gradeService.selectById(gradeId);
		if (Objects.isNull(grade)) {
			throw RWYException.create(403);
		}
		SecurityGradePrice discount = gradePriceService.selectOne(new EntityWrapper<SecurityGradePrice>().where("grade_id", gradeId)); 
		SecurityGradePriceDTO price = BeanUtils.copyProperties(discount, SecurityGradePriceDTO.class);
		price.setName(grade.getName());
		json.setData(price);
		setJsonMessage(json);
		return json;
		
	}
	


	private void initData(SecurityGradeDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		param.buildCreateMsg(user.getId(), user.getUsername()).buildUpdateMsg(user.getId(), user.getUsername());
	} 
	
	private Predicate<SecurityGradeDTO> validate() {
		return  s -> StringUtil.isEmpty(s.getName()) || CollectionUtil.isEmpty(s.getChannelFeeRebateList()) 
				   || s.getChannelFeeRebateList().parallelStream().anyMatch(x -> StringUtil.isEmpty(x.getFeeId())
						   || ObjectUtil.isEmpty(x.getChannelId()) || ObjectUtil.isEmptyOrLtZero(x.getRebate()));
				
	}
}
